package com.wg.dome.handle;

import com.wg.dome.ClientPeer;
import com.wg.dome.ErrorCode;
import com.wg.dome.ErrorHelper;
import com.wg.dome.annotation.Request;
import com.wg.dome.entity.config.EquipQiangHuaEntity;
import com.wg.dome.entity.user.EquipEntity;
import com.wg.dome.entity.user.PlayerEntity;
import com.wg.dome.operation.OperationCode;
import com.wg.dome.operation.QiangHuaRequest;
import com.wg.dome.operation.EquipQiangHuaResponse;
import com.wg.dome.repository.*;
import com.wg.common.net.OperationRequest;
import com.wg.common.net.OperationResponse;
import com.wg.common.net.SendParameters;

/**
 * 装备强化
 */
@Request(OperationCode = OperationCode.QIANG_HUA)
public class HandleQiangHuaRequest extends BaseHandle {

    @Override
    public OperationResponse execute(ClientPeer peer, OperationRequest request, SendParameters sendParameters) {

        QiangHuaRequest qiangHuaRequest = new QiangHuaRequest(request);
        if (!qiangHuaRequest.isValid()) {
            return ErrorHelper.getInvalidResponse(qiangHuaRequest);
        }

        if (qiangHuaRequest.getPlayerId() != peer.getPlayerId()) {
            return ErrorHelper.getErrorResponse(request.getOperationCode(),
                    ErrorCode.PLAYERID_MATCH_ERROR, qiangHuaRequest.getPlayerId(), peer.getPlayerId());
        }

        PlayerRepository playerRepository = new PlayerRepository();
        PlayerEntity playerEntity = playerRepository.get(qiangHuaRequest.getPlayerId());
        long gold = playerEntity.getGold();

        EquipRepository equipRepository = new EquipRepository();
        EquipEntity equipEntity = equipRepository.getEntity(qiangHuaRequest.getPlayerId(), qiangHuaRequest.getEquipId());

        if (equipEntity == null) {
            return ErrorHelper.getErrorResponse(request.getOperationCode(),
                    ErrorCode.EQUIP_ERROR, qiangHuaRequest.getEquipId());
        }

        short qhlv = equipEntity.getQhLv();

        EquipQiangHuaRepository equipQiangHuaRepository = new EquipQiangHuaRepository();
        EquipQiangHuaEntity equipQiangHuaEntity = equipQiangHuaRepository.getNextEntiry(qhlv);
        if (equipQiangHuaEntity == null) {
            return ErrorHelper.getErrorResponse(request.getOperationCode(),
                    ErrorCode.LEVEL_MAX);
        }

        if (equipQiangHuaEntity.getUseGold() > gold) {
            return ErrorHelper.getErrorResponse(request.getOperationCode(),
                    ErrorCode.GOLD_LIMIT);
        }

        long willUseGold = 0;
        long useGold = 0;

        for (short i = equipEntity.getQhLv(); i < equipEntity.getQhLv() + 5; i++) {
            EquipQiangHuaEntity qiangHuaEntity = equipQiangHuaRepository.getNextEntiry(i);
            if (qiangHuaEntity == null){
                break;
            }
            willUseGold += qiangHuaEntity.getUseGold();
            if (willUseGold > gold) {
                break;
            }
            useGold += qiangHuaEntity.getUseGold();
            qhlv++;
        }


        playerEntity.setGold(gold - useGold);
        playerRepository.update(playerEntity);

        equipEntity.setQhLv(qhlv);
        equipRepository.update(equipEntity);

        EquipQiangHuaResponse response = new EquipQiangHuaResponse();
        response.setEquipId(qiangHuaRequest.getEquipId());
        response.setGold(playerEntity.getGold());
        response.setLevel(qhlv);

        return new OperationResponse(request.getOperationCode(), response);
    }
}
